package com.superteam.zzl.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;

import java.util.HashMap;
import java.util.Objects;
import java.util.Set;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import jp.wasabeef.glide.transformations.CropCircleTransformation;

public class QQLoginActivity extends AppCompatActivity implements PlatformActionListener{

    private Handler handler;
    private Intent intent;
    private Set<String> keys;
    private HashMap<String,Objects> map;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_qqlogin);
        init();
        login();
    }

    private void init() {
        map = new HashMap<>();
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                //    Set<String> keys=()msg
                map = (HashMap<String, Objects>) msg.obj;
                //    Set<String> keys=map.keySet();
                //tv.setText(String.valueOf(map.get("nickname")));
                //final BitmapPool bitmapPool = Glide.get(getApplicationContext()).getBitmapPool();
//                Glide.with(getApplicationContext()).load(map.get("figureurl_qq_2"))
//                        .bitmapTransform(new CropCircleTransformation(bitmapPool)).into(iv);
                Log.e("handleMessage: ", String.valueOf(map.get("nickname")));
                intent = getIntent();
                intent.putExtra("name",map.get("nickname").toString());
                intent.putExtra("pic",map.get("figureurl_qq_2").toString());
                setResult(258,intent);
                finish();


            }
        };
    }

    private void login()
    {
        //获取对应的平台对象
        Platform pf= ShareSDK.getPlatform(QQ.NAME);
        pf.authorize();//调用授权
        //设置登陆结果的回调接口
        pf.setPlatformActionListener(this);
        pf.showUser(null);//显示用户信息
    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {

        keys=hashMap.keySet();
        for (String key:keys)
        {
            Log.e("TS", "onComplete: 键="+key+",值="+hashMap.get(key));
        }
        Message msg=handler.obtainMessage(0x110);
        msg.obj=hashMap;
        handler.sendMessage(msg);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {

    }

    @Override
    public void onCancel(Platform platform, int i) {

    }
}
